#define _CRT_SECURE_NO_WARNINGS 1

struct ListNode* deleteDuplicates(struct ListNode* head) {
    if (head == NULL || head->next == NULL)
    {
        return head;
    }
    struct ListNode* newhead = NULL;
    int val = head->val;
    struct ListNode* pcur = head;
    int he_pv = 0;

    struct ListNode* pnext = pcur->next;
    struct ListNode* prev = NULL;
    while (1)
    {
        if (prev && newhead == NULL)
        {
            newhead = prev;
        }
        if (val != pnext->val && pcur == head)
        {
            newhead = prev = pcur;
        }
        val = pcur->val;
        while (pnext && val == pnext->val)
        {
            pnext = pnext->next;
        }
        if (prev == NULL && pnext == NULL)
        {
            break;
        }
        if (pnext == NULL)
        {
            prev->next = NULL;
            break;
        }
        if (prev == NULL && pnext->next == NULL)
        {
            newhead = prev = pnext;
            break;
        }
        if (pnext->next == NULL)
        {
            prev->next = pnext;
            break;
        }
        if (pnext && pnext->next && pnext->val != pnext->next->val)
        {
            if (prev == NULL)
            {
                prev = pnext;
            }
            else
            {
                prev->next = pnext;
                prev = pnext;
            }
        }
        pcur = pnext;
        pnext = pnext->next;
    }

    return newhead;
}